package controllers;

import gui.*;
import hibernate.Activity;
import hibernate.Schedule;
import hibernate.ScheduledActivity;
import hibernate.User;
import interfaces.IDbController;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import java.io.File;
import java.util.List;
import javax.swing.*;

/**
 *
 * @author Kurt
 */
public final class GUIController implements IDbController {

    JFrame frame;
    JPanel panel;
    JDesktopPane desk;
    DbController dbC;
    ScheduleController schedC;
    public User currentUser;

    public GUIController(DbController d, ScheduleController s) {
        dbC = d;
        schedC = s;
        createDesktop(new ALogin(this));      //*Commented out for database testing
        //runDbTest();
    }

    public void createDesktop(JInternalFrame gui) {
        frame = new JFrame("OnPoint");
        if (gui instanceof ALogin) {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        } else {
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        }
        panel = new JPanel();
        desk = new JDesktopPane();
        frame.add(desk);
        frame.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);//set the frame to full size
        desk.setPreferredSize(desk.getMaximumSize());
        frame.setVisible(true);
        showGUI(gui);
    }

    // given the JInternalFrame, this function makes the jInternalFrame visible
    public void showGUI(JInternalFrame gui) {
        //immobileFrame(gui);
        desk.add(gui);
        gui.setVisible(true);
        try {
            gui.setMaximum(true);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

    //this method is used for when the launch button is pressed
    //it is similar to the createDesktop used to start the program
    //but is used to launch the student view of the program
    public void createLaunch() {
        frame = new JFrame();
        desk = new JDesktopPane();
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.add(desk);
        frame.setVisible(true);
        showGUI(new ALogin(this));

    }

    //This function renders the JInternalFrames immobile so they do not move in the desktop
    public void immobileFrame(JInternalFrame gui) {
        for (MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) gui.getUI()).getNorthPane().getMouseListeners()) {

            ((javax.swing.plaf.basic.BasicInternalFrameUI) gui.getUI())
                    .getNorthPane().removeMouseListener(listener);
        }
    }

    public Boolean processLogin(User u) {   //Kurt
        //Calls dbController to validate user login
        currentUser = dbC.validLogin(u);
        if (currentUser != null) {
            showGUI(new AMyClass(this));
            return true;
        }
        currentUser = null;
        return false;
    }

    public List<User> getStudentList(User u){       //User  needs to be a Teacher
        return schedC.getStudentList(u);
    }
        
    public List<Activity> getActivityList(){
        return schedC.getActivityList();
    }

    public boolean isObjectUnique(List l, Object o){
        return dbC.isObjectUnique(l, o);
    }
    
    public Activity copyActivityImageAndSoundFiles(File image, File sound, Activity o){
        o = (Activity) dbC.copyImageAndSoundFiles(image, sound, o);
        return o;
    }
    
    public User copyStudentImageFiles(File image, User u){
        u = (User) dbC.copyImageAndSoundFiles(image, null, u);
        return u;
    }

    @Override
    public List getDbData(Class c, Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void updateDbData(Object o){
        dbC.updateDbData(o);
    }
    
    public void deleteDbData(Object o){
        dbC.deleteDbData(o);
    }
    
    @Override
    public int getDbDataCount(Class c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void saveDbData(Object o) {
        dbC.saveDbData(o);
    }
    
    public void saveActivities(List<ScheduledActivity> s){
        schedC.saveScheduledActivities(s);
    }

}